package com.beyond.pm.ui.test

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.grid.GridCells
import androidx.compose.foundation.lazy.grid.LazyVerticalGrid
import androidx.compose.foundation.lazy.grid.itemsIndexed
import androidx.compose.material3.Button
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Icon
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Slider
import androidx.compose.material3.SliderDefaults
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableFloatStateOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.runtime.staticCompositionLocalOf
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalConfiguration
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import com.beyond.common.ext.toColorOrNull
import com.beyond.common.local.LocalCustomPrimaryColor
import com.beyond.pm.bean.RouteScreenParameter
import com.beyond.pm.component.MyTopBar
import com.beyond.pm.component.MyTopBarStyle
import com.kyant.monet.toSrgb
import java.util.Locale

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun TestPage() {
    Scaffold(
        topBar = {
            MyTopBar(
                style = MyTopBarStyle.CenterAligned,
                title = {
                    Row(
                        modifier = Modifier.fillMaxWidth(),
                        horizontalArrangement = Arrangement.Center,
                        verticalAlignment = Alignment.CenterVertically
                    ) {
                        Icon(
                            imageVector = RouteScreenParameter.Config.bean.icon,
                            contentDescription = null,
                        )
                        Spacer(modifier = Modifier.width(10.dp))
                        Text(text = stringResource(id = RouteScreenParameter.Config.bean.labelId))
                    }
                },
            )
        },
    ) {
        ConfigContent(it)
    }
}

@Composable
private fun ConfigContent(paddingValues: PaddingValues) {
    LazyColumn(
        modifier = Modifier
            .fillMaxSize()
            .padding(16.dp),
        contentPadding = PaddingValues(
            start = 16.dp,
            top = paddingValues.calculateTopPadding(),
            end = 16.dp,
            bottom = paddingValues.calculateBottomPadding()
        ),
    ) {
        item {
//            LanguageSwitchExample()
        }
    }

}

@Composable
private fun ColorsContent() {
    val colors = generateColors()
    LazyVerticalGrid(
        modifier = Modifier
            .fillMaxSize()
            .padding(16.dp),
        columns = GridCells.Adaptive(20.dp),
        verticalArrangement = Arrangement.spacedBy(1.dp),
        horizontalArrangement = Arrangement.spacedBy(1.dp)
    ) {
        itemsIndexed(
            items = colors,
        ) { _, item ->
            ColorItem(color = item)
        }

    }
}

@Composable
fun ColorItem(color: Color) {
    Box(
        modifier = Modifier
            .size(20.dp)
            .background(color)
    )
}

fun generateColors(): List<Color> {
    val colors = mutableListOf<Color>()
    val step = 255 / 5
    for (r in 0 until 5) {
        for (g in 0 until 5) {
            for (b in 0 until 5) {
                colors.add(
                    Color(
                        red = (r * step).toFloat() / 255f,
                        green = (g * step).toFloat() / 255f,
                        blue = (b * step).toFloat() / 255f,
                        alpha = 1f
                    )
                )
            }
        }
    }
    return colors
}

@Composable
fun ColorSlider() {
    val customPrimaryColor = LocalCustomPrimaryColor.current
    val localColor = customPrimaryColor.toColorOrNull() ?: Color.Transparent
    var rSliderPosition by remember { mutableFloatStateOf(localColor.red) }
    var gSliderPosition by remember { mutableFloatStateOf(localColor.green) }
    var bSliderPosition by remember { mutableFloatStateOf(localColor.blue) }
    val color = calculateColor(rSliderPosition, gSliderPosition, bSliderPosition)

    Column(
        modifier = Modifier
            .fillMaxSize(),
        horizontalAlignment = Alignment.CenterHorizontally
    ) {
        Slider(
            value = rSliderPosition,
            colors = SliderDefaults.colors(thumbColor = Color(rSliderPosition, 0f, 0f, 1f)),
            onValueChange = { newPosition ->
                rSliderPosition = newPosition
            }
        )
        Slider(
            value = gSliderPosition,
            colors = SliderDefaults.colors(thumbColor = Color(0f, gSliderPosition, 0f, 1f)),
            onValueChange = { newPosition ->
                gSliderPosition = newPosition
            }
        )
        Slider(
            value = bSliderPosition,
            colors = SliderDefaults.colors(thumbColor = Color(0f, 0f, bSliderPosition, 1f)),
            onValueChange = { newPosition ->
                bSliderPosition = newPosition
            }
        )
        Text(
            modifier = Modifier
                .fillMaxWidth()
                .padding(top = 10.dp)
                .height(50.dp)
                .background(color)
                .padding(top = 12.dp),
            text = color.toSrgb().toHex(),
            color = Color.White,
            textAlign = TextAlign.Center,
        )
    }
}

fun calculateColor(red: Float, green: Float, blue: Float): Color {
    return Color(red, green, blue, 1f)
}
